package com.voodoo.lib_frame.manager;

import android.app.Activity;
import android.os.Process;

import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;
import java.util.Stack;

/**
 * Author: voodoo
 * CreateDate: 2021/05/16
 * Description: Activity管理类
 */
public class ActivityManager {

    private Stack<Activity> activitys;
    private static ActivityManager activityManager;

    public static ActivityManager getInstance() {
        if (activityManager == null) {
            synchronized (ActivityManager.class) {
                if (activityManager == null) {
                    activityManager = new ActivityManager();
                }
            }
        }
        return activityManager;
    }

    /**
     * 获取Activity栈
     *
     * @return {@link Stack}
     */
    public Stack<Activity> getActivitys() {
        return activitys;
    }

    /**
     * 将Activity添加到栈中
     *
     * @param activity 要添加的Activity
     */
    public void addActivity(Activity activity) {
        if (activitys == null) {
            activitys = new Stack<>();
        }
        activitys.push(activity);
    }

    /**
     * 获取栈顶Activity
     *
     * @return Activity
     */
    public Activity getTopActivity() {
        Activity activity = null;
        try {
            activity = activitys.peek();
        } catch (EmptyStackException e) {
            System.out.println("栈为空");
        }
        return activity;
    }

    /**
     * 移除指定Activity
     *
     * @param activity 要移除的Activity
     */
    public void killActivity(Activity activity) {
        if (activitys.empty()) {
            return;
        }
        int activityIndex = activitys.indexOf(activity);
        if (activityIndex != -1) {
            Activity tempActivity = activitys.get(activityIndex);
            tempActivity.finish();
            activitys.remove(activity);
        }
    }

    /**
     * 移除栈顶以外的其他Activity
     */
    public void killOtherActivity() {
        if (activitys.empty()) {
            return;
        }
        // 查找栈顶Activity以外的其他Activity并创建临时集合
        String simpleName = getTopActivity().getClass().getName();
        List<Activity> activityList = new ArrayList<>();
        for (Activity activity : activitys) {
            if (!simpleName.equals(activity.getClass().getName())) {
                activityList.add(activity);
            }
        }
        // 遍历创建的临时集合，将其Activity删除
        for (Activity activity : activityList) {
            killActivity(activity);
        }
        // 清空临时的集合
        activityList.clear();
    }

    /**
     * 移除所有Activity
     * 不退出虚拟机主要用在结束所有Activity之后，还可以跳转指定界面
     * 例如单点登录，结束所有Activity之后跳转回登录界面
     *
     * @param isExit 是否退出程序（结束虚拟机）
     */
    public void killAllActivity(boolean isExit) {
        if (activitys.empty()) {
            return;
        }
        // 查找栈顶Activity以外的其他Activity并创建临时集合
        String simpleName = getTopActivity().getClass().getName();
        List<Activity> activityList = new ArrayList<>(activitys);
        // 遍历创建的临时集合，将其Activity删除
        for (Activity activity : activityList) {
            killActivity(activity);
        }
        // 清空临时的集合
        activityList.clear();
        if (isExit) {
            Process.killProcess(Process.myPid()); // 结束当前程序进程
            System.exit(0); // 销毁虚拟机
        }
    }

}
